package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.school.entity.ClassTeacherLession;
import cn.stylefeng.guns.modular.school.entity.CourseSign;
import cn.stylefeng.guns.modular.school.entity.StudentInfo;
import cn.stylefeng.guns.modular.school.entity.TeacherInfo;
import cn.stylefeng.guns.modular.school.model.params.ClassSchoolTableParam;
import cn.stylefeng.guns.modular.school.model.params.ClassTeacherLessionParam;
import cn.stylefeng.guns.modular.school.model.params.ExamScoreParam;
import cn.stylefeng.guns.modular.school.model.result.CourseSignResult;
import cn.stylefeng.guns.modular.school.model.result.ExamScoreResult;
import cn.stylefeng.guns.modular.school.service.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/exam/score")
public class ApiNeedExamScoreController extends BaseController {

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private ClassTeacherLessionService classTeacherLessionService;
    @Autowired
    private ExamScoreService examScoreService;

    @RequestMapping(value = "list")
    public Object list(String userCode) {
        Map map = new HashMap();

        StudentInfo student = studentInfoService.findByCode(userCode);
        if (student == null) {
            ClassTeacherLessionParam classTeacherLessionParam = new ClassTeacherLessionParam();
            TeacherInfo teacherInfo = teacherInfoService.findByCode(userCode);
            classTeacherLessionParam.setTeacherId(teacherInfo.getId());
            // 教师教课的所有班
            List<ClassTeacherLession> classes = classTeacherLessionService.findByTeacherId(classTeacherLessionParam);
            List<Integer> classIds = new ArrayList<>();
            classes.forEach(classTeacherLession -> {
                classIds.add(classTeacherLession.getClassId());
            });
            List<StudentInfo> studentInfoList = studentInfoService.list(new QueryWrapper<StudentInfo>().in("class_id", classIds));
            if (studentInfoList.size() > 0) {
                List<Integer> studentIds = new ArrayList<>();
                studentInfoList.forEach(studentInfo -> {
                    studentIds.add(studentInfo.getId());
                });
                ExamScoreParam param = new ExamScoreParam();
                param.setStudentIds(studentIds);
                List<ExamScoreResult> examScoreResults = examScoreService.findListByStudentIds(param);
                map.put("examScores", examScoreResults);
            }
        } else {
            ExamScoreParam param = new ExamScoreParam();
            param.setStudentId(student.getId());
            List<ExamScoreResult> examScoreResults = examScoreService.findListByStudentId(param);
            map.put("examScores", examScoreResults);
        }


        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

}


